package com.example.reggie.utils;


import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;

import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import org.apache.commons.lang.StringUtils;

/**
 * 短信发送工具类
 */
public class SMSUtils {

		public static void send(String phone, String code) {
			if(StringUtils.isEmpty(phone))
			{
				System.out.println("手机号不合法");
			}
//			txyun.sms.SecretId =AKID8HPVYcdcepeXSPMF2EFJh6UJ9zwx4Ikb
//			txyun.sms.SecretKey=3AHMNUWZepXUuMkJ1jNuM442vkTDpL9w
//			txyun.sms.smsapiurl=sms.tencentcloudapi.com
//			txyun.sms.TemplateID=1391628
//			txyun.sms.Sign=km的编程之旅公众号
//			txyun.sms.SmsSdkAppid=1400674357
			Credential cred = new Credential("AKID8HPVYcdcepeXSPMF2EFJh6UJ9zwx4Ikb", "3AHMNUWZepXUuMkJ1jNuM442vkTDpL9w");

			HttpProfile httpProfile = new HttpProfile();
			httpProfile.setReqMethod("POST");
			httpProfile.setConnTimeout(60);
			httpProfile.setEndpoint("sms.tencentcloudapi.com");

			ClientProfile clientProfile = new ClientProfile();
			clientProfile.setSignMethod("HmacSHA256");
			clientProfile.setHttpProfile(httpProfile);
			SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
			SendSmsRequest req = new SendSmsRequest();

			String smsSdkAppid = "1400674357";
			req.setSmsSdkAppId(smsSdkAppid);

			req.setSignName("km的编程之旅公众号");
			req.setTemplateId("1391628");
			req.setPhoneNumberSet(new String[]{"+86" + phone});
			String[] parm = {code};
			req.setTemplateParamSet(parm);

			SendSmsResponse res= null;
			try {
				res= client.SendSms(req);
				System.out.println(res);
				System.out.println(SendSmsResponse.toJsonString(res));
			} catch (TencentCloudSDKException e) {
				e.printStackTrace();
			}

//        Map<String,Object> param = new HashMap();
//        param.put("code",code);
//        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

		}
	}

